<template>
  <div class="business-payment-container">
    <!-- <top-tip :tipcontent="tipcontent" /> -->
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <!-- <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.cityArea"
                :options="cityOptions"
                :props="citySelect"
                placeholder="选择地区"
                clearable
              />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.industry"
                :options="industryOptions"
                :props="industrySelect"
                placeholder="选择行业"
                clearable
              />
            </el-form-item> -->
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="按客户名称"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.pickStaff"
                clearable
                placeholder="按签约人"
              />
            </el-form-item>
            <!-- <el-form-item>
              <el-input
                v-model.trim="queryForm.storeAccount"
                clearable
                placeholder="请输入店铺账号"
              />
            </el-form-item> -->
            <el-form-item>
              <el-input
                v-model.trim="queryForm.orderCode"
                clearable
                placeholder="按合同编号"
              />
            </el-form-item>
            <el-form-item>
              <!-- <el-date-picker
                v-model="queryForm.orderDate"
                type="date"
                placeholder="请选择确认收款时间"
              /> -->
              <DateSelect ref="DateSelectRef" @get-date="getDate" />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="新单待确认收款" name="new">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="收款方式" width="200">
            <template #default="{ row }">
              <div v-for="item in row.OrderPayments" :key="item.PaymentID">
                {{ item.PaymentType }}
              </div>
              <!-- {{
                row.OrderFlow.find((item) => item.OrderFlowState === 3)?.addTime
              }} -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="到单录入时间" width="200">
            <template #default="{ row }">
              <div v-for="item in row.OrderFlow" :key="item.OrderFlowID">
                <div v-if="item.OrderFlowState === 2">
                  {{ item.addTime }}
                </div>
              </div>
              <!-- {{
                row.OrderFlow.find((item) => item.OrderFlowState === 3)?.addTime
              }} -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                审核并确认
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业绩核算" width="160">
            <template #default="{ row }">
              <div v-if="row.querenYeji">
                <span v-if="row.querenYeji == 1">核算业绩</span>
                <span v-else>不核算业绩</span>
                <br />
                {{ row.QuerenYejiStaffName }}
                <br />
                {{ row.QuerenYejiTime }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已确认收款" name="paymented">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="收款方式" width="200">
            <template #default="{ row }">
              <div v-for="item in row.OrderPayments" :key="item.PaymentID">
                {{ item.PaymentType }}
              </div>
              <!-- {{
                row.OrderFlow.find((item) => item.OrderFlowState === 3)?.addTime
              }} -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="确认收款时间" width="260">
            <template #default="{ row }">
              <div v-for="item in row.OrderPayments" :key="item.PaymentID">
                <div v-if="item.PaymentState">
                  （{{ item.PaymentStaffName }}）{{ item.PaymentTime }}
                </div>
              </div>
              <!-- {{
                row.OrderFlow.find((item) => item.OrderFlowState === 3)?.addTime
              }} -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="业绩核算" width="160">
            <template #default="{ row }">
              <div v-if="row.querenYeji">
                <span v-if="row.querenYeji == 1">核算业绩</span>
                <span v-else>不核算业绩</span>
                <br />
                {{ row.QuerenYejiStaffName }}
                <br />
                {{ row.QuerenYejiTime }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { CitySelect, IndustrySelect } from '@/api/common'
  import DateSelect from '/src/components/DateSelect.vue'
  import { getBusinessList } from '@/api/businessOrder'
  import Edit from './components/BusinessPaymentEdit'
  // import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'Payment',
    components: { Edit, DateSelect },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        cityOptions: [],
        DateSelectRef: null,
        industryOptions: [],
        citySelect: { checkStrictly: true, multiple: true },
        industrySelect: { checkStrictly: true, multiple: true },
        // tipcontent: {
        //   title: '关于确认收款',
        //   memo:
        //     '本页用于出纳人员确认合同业务单收款；默认显示新提单未确认收款的合同业务单；<br>' +
        //     '点开需要审核确认收款的业务合同单，即可查看此业务合同的具体详情，同时可以看到相关款项信息；<br>' +
        //     '点击确认收款，填写相关收款信息，提交确认，并通知到相关人员。',
        // },
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,

        selectRows: '',
        queryForm: {
          dataType: 'payment',
          tab: 'new',
          pageNo: 1,
          pageSize: 10,
          cityArea: {},
          industry: {},
          searchKey: '',
          orderCode: '',
          pickStaff: '',
          searchOrderFlow: 3,
          orderDate: '',
        },
        activeName: 'new',
      })

      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const revieworder = (row) => {
        router.push({
          path: 'reviewOrder',
          query: { businessid: row.businessid },
        })
      }
      const handleEdit = (row) => {
        router.push({
          path: 'paymentOrder',
          query: { orderid: row.OrderID },
        })
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getBusinessList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }

      onMounted(() => {
        fetchData()
        getCitySelect()
        getIndustrySelect()
      })
      const getCitySelect = async () => {
        const {
          data: { cityOptions },
        } = await CitySelect()
        state.cityOptions = cityOptions
        console.log('列表1', cityOptions)
      }
      const getIndustrySelect = async () => {
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.industryOptions = industryOptions
        console.log('列表2', industryOptions)
      }
      const getDate = (data) => {
        state.queryForm.orderDate = data
        console.log(data, '子组件传过来的时间数组')
      }

      return {
        ...toRefs(state),
        handleTabClick,
        setSelectRows,
        getCitySelect,
        getIndustrySelect,
        getDate,
        revieworder,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>

<style></style>
